package mju.cxf.controller.user;
import mju.cxf.dto.EmailDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mju.cxf.exception.BaseException;
import mju.cxf.result.Result;
import mju.cxf.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import java.io.UnsupportedEncodingException;

/**
 * 邮箱相关接口
 */
@RestController
@RequestMapping("/user/api/email")
@Api(tags = "邮箱接口列表")
public class IEmailController {

    private final EmailService emailService;

    @Autowired
    public IEmailController(EmailService emailService) {
        this.emailService = emailService;
    }

    /**
     * 获取邮箱验证码
     * @param emailDTO
     * @return
     */
    @PostMapping("/getCode")
    @ApiOperation(value = "获取登陆验证码",tags = "发送邮件以获取登陆验证码")
    public Result<String> putCode(@RequestBody(required = true) EmailDTO emailDTO) {
        try {
            if (emailDTO.getTo()==null){
                return Result.error("不能为空");
            }
            emailService.getEmailCode(emailDTO);
        } catch (MessagingException | UnsupportedEncodingException e) {
            throw new BaseException("操作失败");
        }
        return Result.success("操作成功");
    }
    /**
     * 获取邮箱验证码
     * @param emailDTO
     * @return
     */
    @PostMapping("/getKeyCode")
    @ApiOperation(value = "获取登陆验证码",tags = "发送邮件以获取登陆验证码")
    public Result<String> putKeyCode(@RequestBody(required = true) EmailDTO emailDTO) {
        if (emailDTO.getTo()==null){
            return Result.error("不能为空");
        }
        emailService.getKey(emailDTO);
        return Result.success("操作成功");
    }

}
